#include<bits/stdc++.h>
using namespace std;
struct node{
	int v,a,b,c,d,e;
}x[5005],surver[5005];
int n,k,cnt;
string s,str;



int strcompare(string p,string q){
	if(p.size()!=q.size()) return 0;
	for(int i=0;i<=p.size();i++){
		if(p[i]!=q[i]) return 0;
	}
	return 1;
}



void zhao(int value, int times){
	if(times == 1) x[k].a = value;
	if(times == 2) x[k].b = value;
	if(times == 3) x[k].c = value;
	if(times == 4) x[k].d = value;
	if(times == 5) x[k].e = value;
}



int pdhf(string s){
	if(s.size() < 9 || s.size() > 21) return 0;
	int po=0,tmp=0,w=0,flag=1,symflag=1,maohao=0,dianshu=0;
	for(int i=0;i<s.size();i++){
		if((s[i]<'0'||s[i]>'9')&&(s[i]!='.'&&s[i]!=':')&&s[i]!=' ') symflag=0;
		if(s[i]=='.') dianshu++;
		if(s[i] == '.' || s[i] == ':'){
			if(s[i+1]=='.'||s[i+1]==':') return 0;
			tmp=0;
			if(po == '0') flag = 0;
			for(int j=po;j<i;j++){
				tmp *= 10;
				tmp += (s[j]-'0');
//				cout<<s[j]<<" "<<tmp<<"   ";
			}
			w++;
			zhao(tmp,w);
//			cout<<endl<<endl;
//			cout<<tmp<<endl;
			if(tmp >= 256) flag=0;
			po=i+1;
		}
		if(s[i] == ':'){
			if(maohao==1) return 0;
			maohao=1;
			tmp = 0;
			for(int j=i+1;j<s.size();j++){
				tmp *= 10;
				tmp += (s[j]-'0');
			}
			w++;
			zhao(tmp,w);
//			cout<<tmp<<endl;
			if(tmp >= 65536) flag=0;
		}
	}
	if(maohao==0||flag==0||symflag==0||dianshu!=3) return 0;
	else return 1;
}


int check(int k){
	for(int i=1;i<=cnt;i++){
		if(x[k].a == surver[i].a && x[k].b == surver[i].b && x[k].c == surver[i].c && x[k].d == surver[i].d && x[k].e == surver[i].e){
			return surver[i].v;
		}
	}
	return 0;
}


void solve(int i){
	int tmp=check(i);
	if(x[i].v == 1){
		if(!tmp){
			cnt++;
			surver[cnt].a = x[i].a, surver[cnt].b = x[i].b, surver[cnt].c = x[i].c, surver[cnt].d = x[i].d, surver[cnt].e = x[i].e, surver[cnt].v=i;
			printf("OK\n");
			return;
		}
		else{
			printf("FAIL\n");
			return;
		}
	}
	else{
		if(!tmp){
			printf("FAIL\n");
			return;
		}
		else{
			printf("%d\n",tmp);
			return;
		}
	}
}



int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		k++;
		cin>>s>>str;
//		cout<<s<<" "<<str<<endl;
		if(!pdhf(str)){
			printf("ERR\n");
			continue;
		}
		if(strcompare(s,"Server")) x[k].v=1;
		else x[k].v=0;
		solve(i);
	}
//	for(int i=1;i<=n;i++){
//		printf("%d %d %d %d %d %d\n",x[i].v,x[i].a,x[i].b,x[i].c,x[i].d,x[i].e);
//	}
//	printf("\n\n");
//	for(int i=1;i<=cnt;i++){
//		printf("%d %d %d %d %d %d\n",surver[i].v,surver[i].a,surver[i].b,surver[i].c,surver[i].d,surver[i].e);
//	}
	return 0;
}
//0.0.0.0:0
//255.255.255.255:65535
